﻿using Core.Comamad;
using Core.ReadAndWrite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Core
{
    public class QuanLyLop : IQuanLi
    {
        #region Singleton
        private static QuanLyLop _Instance;
        private static object locks = new object();

        public static QuanLyLop Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (locks)
                    {
                        if (_Instance == null)
                            _Instance = new QuanLyLop();
                    }
                }
                return _Instance;
            }
        }
        #endregion

        #region Command
        protected List<ICommand> _commands = new List<ICommand>();
        public int Current
        {
            get
            {
                return _commands.Count;

            }
            set
            {
                Current = value;
            }
        }
        public void Undo()
        {
            if (Current > 0)
            {
                var count = (int)Current - 1;
                ICommand cms = _commands[count];
                cms.UnExecute();
            }
        }
        #endregion

        public List<BaseData> data;
        public QuanLyLop()
        {
            if (DataSource.Instance.ReadLopHoc("DsLop.txt") != null)
                data = DataSource.Instance.ReadLopHoc("DsLop.txt");
            else
                data = new List<BaseData>();
        }

  

        public LopHoc this[int index]
        {
            get
            {

                return data[index] as LopHoc;
            }
        }

        public void Add(BaseData ob)
        {
            LopHoc sender = ob as LopHoc;
            BaseData lh = Find(x => (x as LopHoc).MaLop == sender.MaLop);
            if (lh == null)
            {
                data.Add(ob);
                ICommand temp = new ObCommand(LoaiXuLi.Add, ob);
                QuanLyLop.Instance._commands.Add(temp);
            }

        }

        public void Delete(BaseData ob)
        {
            data.Remove(ob);
            ICommand temp = new ObCommand(LoaiXuLi.Remove, ob);
            QuanLyLop.Instance._commands.Add(temp);
        }


        public void Update(BaseData ob)
        {
            LopHoc lh = ob as LopHoc;
            LopHoc find = data.Find(c => (c as LopHoc).MaLop == lh.MaLop) as LopHoc;
            LopHoc t = new LopHoc
            {
                MaLop = find.MaLop,
                TenLop = find.TenLop,
                MaKhoa = find.MaKhoa
            };
            find.TenLop = lh.TenLop;

            find.SiSo = lh.SiSo;
            find.MaKhoa = lh.MaKhoa;

            ICommand tem = new ObCommand(LoaiXuLi.Update, t);
            QuanLyLop.Instance._commands.Add(tem);

        }

        public List<BaseData> FindAll(Predicate<BaseData> Predicate)
        {
            return data.FindAll(Predicate);
        }

        public BaseData Find(Func<BaseData, bool> predicate)
        {
            return data.FirstOrDefault(predicate);
        }
        public override string ToString()
        {
            StringBuilder tem = new StringBuilder();
            foreach (var item in this.data)
            {
                tem.AppendLine(item.ToString());
            }
            return tem.ToString();
        }
    }

}
